package com.tc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    /**
     * *swagger会帮助我们生成接口文档
     * 1.配置生成的文档信息，如文档名称，作者，版本号等
     * 2.配置生成规则
     */
    @Bean
    public Docket getDocket() {
        //创建封面信息对象
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        apiInfoBuilder.title("《医保报销》后端接口文档")//文档标题
                .description("医保报销系统后端接口文档")//文档描述
                .version("v1.0")//版本号
                .contact(new Contact("", "", ""));
        ApiInfo apiInfo = apiInfoBuilder.build();
        
        // 设置全局token参数
        Parameter tokenParam = new ParameterBuilder()
                .name("token")
                .description("用户token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(tokenParam);
        
        //DocumentationType.SWAGGER_2:指定生成的是Swagger2规范的文档风格
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.tc.controller"))
                .paths(PathSelectors.any())
                .build()
                // 支持swagger中文显示
                .useDefaultResponseMessages(false)
                // 支持post请求中RequestBody的注解
                .forCodeGeneration(true)
                .globalOperationParameters(parameters);
    }
}
